<template>
  <a-upload
    v-model:file-list="fileList"
    name="file"
    :maxCount="1"
    action="/api/v1/common/upload-file"
    :headers="headers"
    @change="handleChange"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      {{ $t("351") }}
    </a-button>
  </a-upload>
</template>
<script setup>
import { ref } from "vue";
import CommonDefine from "@/component/CommonDefine";
import { useI18n } from "vue-i18n";
import { UploadOutlined } from "@ant-design/icons-vue";

const { t } = useI18n();
const channel = CommonDefine.channel();
const props = defineProps({ field: Object });
const fileList = ref([]);
const headers = {
  authorization: `Bearer ${localStorage.getItem("token")}`,
};

const handleChange = (info) => {
  if (info.file.status === "done") {
    if (!CommonDefine.useful(channel.uploadData)) {
      channel.uploadData = [];
    }
    channel.uploadData.push({
      fid: props.field.id,
      v: parseInt(info.file.response),
      d: info.file.name,
    });
    CommonDefine.alert(t("352", [info.file.name]));
    // console.log(channel.recordInEditting.values);
  } else if (info.file.status === "error") {
    CommonDefine.alert(t("353", [info.file.name]));
  }
};

const reset = () => {
  fileList.value.splice(0, fileList.value.length);
};

defineExpose({ reset });
</script>
